var vm = new Vue({
    el: '#rrapp',
    data: {
        showList: true,
        isDisable: false, //提交按钮
        title: '',
        loading: false,
        goodsHotsell: [],
        goodsList: [],
        page: {currentPage: 0, pageSizes: [20, 50, 100], pageSize: 20, total: 0},
        q: {goodsId: '', beginCreatetime: '', endCreatetime: ''},
        hotsellFrom: {
            goodsId: '',
            hotValue: '',
            showSort: ''
        },
        dialogFormVisible: false,
        //重置按钮
        resetForm(hotsellFrom) {
            this.$refs[hotsellFrom].resetFields();
        }
    },
    created: function () {
        this.loadData();
    },
    methods: {
        //首页数据
        loadData: function () {
            var params = this.q;
            params.page = this.page.currentPage;
            params.limit = this.page.pageSize;
            $.ajax({
                type: "POST",
                url: baseURL + 'goods/goodshotsell/list',
                contentType: "application/json",
                data: JSON.stringify(params),
                success: function (r) {
                    if (r.code == 0) {
                        vm.goodsHotsell = r.page.list;
                        vm.page.total = r.page.totalCount;
                        vm.goodsList = r.goodsList;
                    } else {
                        alert(r.msg);
                    }
                }
            })
        },
        //序号
        indexMethod: function (index) {
            return index + 1
        },
        //，每页多少数据
        handleSizeChange: function (val) {
            this.page.pageSize = val;
            this.loadData();
        },
        //当前页数据
        handleCurrentChange: function (val) {
            this.page.currentPage = val;
            this.loadData();
        },
        //重置
        reset: function () {
            vm.q.goodsId = '';
            vm.q.beginCreatetime = '';
            vm.q.endCreatetime = '';
            this.loadData();
        },
        //搜索
        search: function () {
            this.page.currentPage = 1;
            vm.loadData();
        },
        //取消清空验证
        closeChangeMachie() {
            vm.dialogFormVisible = false;
            this.$refs['hotsellFrom'].resetFields();
        },
        add: function () {
            this.title = "新 增";
            vm.hotsellFrom = {};
            vm.dialogFormVisible = true;
        },
        handleEdit: function (index, row) {
            var rowData = JSON.parse(JSON.stringify(row));
            vm.hotsellFrom = Object.assign({}, rowData);
            this.title = "编 辑";
            vm.dialogFormVisible = true;
        },

        //远程搜索
        remoteMethod(query) {
            if (query !== '') {
                var item = {'name': query};
                var _this = this;
                _this.loading = true;
                var url = 'goods/goodsrecommend/search';
                setTimeout(function () {
                    _this.loading = false;
                    $.ajax({
                        type: "POST",
                        url: baseURL + url,
                        contentType: "application/json",
                        data: JSON.stringify(item),
                        success: function (r) {
                            if (r.code === 0) {
                                vm.goodsList = r.goodsList;
                            } else {
                                alert(r.msg);
                            }
                        }
                    });
                }, 200);
            }
        },
        ///////////

        //新增编辑提交
        submitForm(hotsellFrom) {
            var postData = {
                id: vm.hotsellFrom.id,
                goodsId: vm.hotsellFrom.goodsId,
                showSort: vm.hotsellFrom.showSort,
                hotValue: vm.hotsellFrom.hotValue
            };
            //输入为负数，不写入数据库
            var url;
            var _this = this;
            if (isBlank(postData.id)) {
                url = "goods/goodshotsell/save";
            } else {
                url = "goods/goodshotsell/update";
            }
            this.$refs[hotsellFrom].validate(function (valid) {
                if (valid) {
                    //输入为负数，不写入数据库
                    if (!validNumber(vm.hotsellFrom.hotValue)) {
                        _this.$message.error('热度   请输入大于0的整数数字');
                        return false;
                    }
                    if (!validNumber(vm.hotsellFrom.showSort)) {
                        _this.$message.error('展示顺序 请输入大于0的整数数字');
                        return false;
                    }
                    $.ajax({
                        type: "POST",
                        url: baseURL + url,
                        contentType: "application/json",
                        data: JSON.stringify(postData),
                        success: function (r) {
                            if (r.code === 0) {
                                vm.dialogFormVisible = false;
                                _this.$message({
                                    type: 'success',
                                    message: '操作成功!'
                                });
                                setTimeout(function () {
                                    vm.loadData()
                                }, 1000)
                            } else {
                                alert(r.msg);
                            }
                        }
                    })
                } else {
                    console.log('error submit!!');
                    return false;
                }
            });
        },
        ///////////////////

        /*  handleDelete: function (index, row) {
              var rowData = JSON.parse(JSON.stringify(row));
              var id = rowData.id;
              var _this = this;
              this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
                  confirmButtonText: '确定',
                  cancelButtonText: '取消',
                  type: 'warning'
              }).then(function () {
                  $.ajax({
                      type: "POST",
                      url: baseURL + "goods/goodshotsell/delete?id="+id,
                      contentType: "application/json",
                      data:null,
                      success: function (r) {
                          if (r.code == 0) {
                              _this.$message({
                                  type: 'success',
                                  message: '删除成功!'
                              });
                              setTimeout(function () {
                                  vm.loadData()
                              }, 1000)
                          } else {
                              alert(r.msg);
                          }
                      }
                  });
              }).catch(function () {
                  _this.$message({
                      type: 'info',
                      message: '已取消删除'
                  });
              });
          }*/
    }
});

//验证数字为大于等于0的飞负整数
function validNumber(str) {
    var reg = /^\+?[1-9][0-9]*$/;
    if (!reg.test(str)) {
        return false;
    } else {
        return true;
    }
}

//控制提交间隔3s
function testButton() {
    var aaa = document.getElementById('click1');
    aaa.disabled = true; //使用true或false，控制是否让按钮禁用
    setTimeout(function () {
        aaa.disabled = false;
    }, 2500);//1秒后才能点击
}
